package com.szqhyxj.youxiangju.biz.receipt.record.fragment.Online;

import com.szqhyxj.youxiangju.biz.receipt.record.ReceiptRecordModel;
import com.szqhyxj.youxiangju.biz.receipt.record.RowsItem;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2019/3/20.
 */

public class OnlineReceiptPresenter implements OnlineReceiptContract.Presenter {
    OnlineReceiptContract.View view;
    int NUM;
    List<RowsItem> list;
    private int PAGENUMBER = 1;
    public OnlineReceiptPresenter(OnlineReceiptContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void init() {
        ReceiptRecordModel.getReceipt(10, 1,"online_channel")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<ReceiptRecordModel.Response>>() {
                    @Override
                    public void accept(BaseResult<ReceiptRecordModel.Response> listBaseResult) throws Exception {
                        if (listBaseResult.businessLogicSuccess()) {

                            NUM = listBaseResult.getData().getTotal() / 10 + 1;

                            List<RowsItem> listrow = listBaseResult.getData().getRows();
                            view.showRecordData(listrow);
                        } else {
                            view.showToast(listBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void load() {
        if (PAGENUMBER < NUM) {
            PAGENUMBER = ++PAGENUMBER;
            ReceiptRecordModel.getReceipt(10, PAGENUMBER,"online_channel")
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Consumer<BaseResult<ReceiptRecordModel.Response>>() {
                        @Override
                        public void accept(BaseResult<ReceiptRecordModel.Response> listBaseResult) throws Exception {
                            if (listBaseResult.businessLogicSuccess()) {

                                List<RowsItem> list = new ArrayList<>();
                                list.addAll(listBaseResult.getData().getRows());
                                view.showLoad(list);
                            } else {
                                view.showToast(listBaseResult.getMessage());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            view.showToast(throwable.getMessage());
                        }
                    });

        } else {
            view.showNodata();
            view.showToast("最后一页");

        }
    }
}
